package com.project.starter.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.starter.entity.Auth;
import com.project.starter.entity.Blog;
import com.project.starter.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;

@Slf4j
@RestController
@RequestMapping("/blog")
@Api("Blog 接口")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private DataSource dataSource;

    @ApiOperation("获取blog")
    @RequestMapping(value = "/getBlogById", method = RequestMethod.GET)
    public Blog getBlogById(@RequestParam("id") Integer id) {
        Blog blog = blogService.getBlogById(id);
        log.info("requestId {}, reponse {}", id, JSONObject.toJSON(blog));
        return blog;
    }

    @ApiOperation("添加blog")
    @RequestMapping(value = "/addBlog", method = RequestMethod.POST)
    public Blog addBlog(@RequestBody Blog blog) {
        blogService.addBlogAndAuth(blog);
        return blog;
    }
    @ApiOperation("添加Auth")
    @RequestMapping(value = "/addAuth", method = RequestMethod.POST)
    public Auth addAuth(Auth auth) {
        blogService.addAuth(auth);
        return auth;
    }

}
